//
//  menuTip.m
//  LOLBox
//
//  Created by mac on 15-3-27.
//  Copyright (c) 2015年 Honey. All rights reserved.
//

#import "menuTip.h"

@interface menuTip ()
{
    UIImageView *_imgView;
    UIView *_btnView;
    
    void (^_valueBlock)(NSInteger);
}

@end

@implementation menuTip

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 80)];
        _btnView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, 300, 50)];
        //_btnView.backgroundColor = [UIColor greenColor];
        _imgView.image = [UIImage imageNamed:@"menu_bg_down"];
        
        [self addSubview:_imgView];
        [self addSubview:_btnView];
        
        [self createSomeBtn];
    }
    return self;
}

- (void)changeMenuTipFrame:(CGRect)rect andViewFrame:(CGRect)ViewRect
{
    //self.backgroundColor = [UIColor blackColor];
    self.alpha = 1;
    //CGRect imgViewRect = _imgView.frame;
    self.frame = [self setMenuRect:rect andViewRect:ViewRect];
    
    

}

- (void)createSomeBtn
{
    
    //toolbar_save
    //video1 video2 video3  biaoqing gaoqing chaoqing
    
    UIImage *imgVideo1 = [UIImage imageNamed:@"vodeo1"];
    UIImage *imgVideo2 = [UIImage imageNamed:@"video2"];
    UIImage *imgVideo3 = [UIImage imageNamed:@"video3"];
    UIImage *imgDetail = [UIImage imageNamed:@"icon_web"];
    UIImage *imgSave = [UIImage imageNamed:@"toolbar_save"];
    UIImage *imgCommon = [UIImage imageNamed:@"contenttoolbar_hd_comment_light"];
    
    NSArray *nameArr = @[@"标清",@"高清",@"超清",@"详情",@"缓存",@"评论"];
    NSArray *iconArr = @[imgVideo1,imgVideo2,imgVideo3,imgDetail,imgSave,imgCommon];
    
    //300  90
    for (NSInteger i = 0; i < 6; i++)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 100 + i;
        [btn setImage:iconArr[i] forState:UIControlStateNormal];
        [btn setTitle:nameArr[i] forState:UIControlStateNormal];
        //btn.backgroundColor = [UIColor redColor];
        btn.titleLabel.font = [UIFont systemFontOfSize:12];
        btn.imageEdgeInsets = UIEdgeInsetsMake(-18, 15, 0, 1);
        btn.titleEdgeInsets = UIEdgeInsetsMake(25, -20, 0, 0);
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        btn.titleLabel.textColor = [UIColor whiteColor];
        btn.frame = CGRectMake(i * 50, 0, 50, 50);
        
        [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
        [_btnView addSubview:btn];
    }
}

- (void)onClick:(UIButton *)sender
{
    NSInteger index = sender.tag - 100;
    _valueBlock(index);
}

- (void)senderIndex:(void (^)(NSInteger))block
{
    _valueBlock = block;
}

//判断菜单的高度
- (CGRect)setMenuRect:(CGRect)rect andViewRect:(CGRect)viewRect;
{
    CGRect tipRect;
    CGFloat viewHeight = CGRectGetHeight(viewRect);
    //NSLog(@"viewHeight:%f",viewHeight);
    
    CGFloat cellHeight = CGRectGetMaxY(rect);
    if (viewHeight - cellHeight - 50 < 80)
    {
        CGRect btnRect = _btnView.frame;
        btnRect.origin.y = 10;
        _btnView.frame = btnRect;
        
        _imgView.image = [UIImage imageNamed:@"menu_bg_up"];
        tipRect = CGRectMake((viewRect.size.width - _imgView.frame.size.width) / 2, CGRectGetMinY(rect) - 80 , viewRect.size.width, 80);
        return tipRect;
    }
    else
    {
        CGRect btnRect = _btnView.frame;
        btnRect.origin.y = 25;
        _btnView.frame = btnRect;
        
        _imgView.image = [UIImage imageNamed:@"menu_bg_down"];
        tipRect = CGRectMake((viewRect.size.width - _imgView.frame.size.width) / 2, CGRectGetMaxY(rect), viewRect.size.width, 80);
        return tipRect;
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
